using System;
using System.Data;

namespace Glpt.Data
{
    /// <summary>
    /// 参数设置类
    /// </summary>
    public class ParamSet
    {
        private static DataSet dset = null;
        private static string filename = "params.xmlzip";

        static ParamSet()
        {
            dset = new DataSet();
            if (System.IO.File.Exists(AbstractInterFace.StartupPath + filename))
            {
                dset = DataSetIO.ReadDataSetFromXmlZip(AbstractInterFace.StartupPath + filename);
            }
            DataTable dt = null;
            if (dset.Tables.Count == 0)
            {
                dt = new DataTable("ParamTable");
                dset.Tables.Add(dt);
            }
            else
                dt = dset.Tables["ParamTable"];
            if (!dt.Columns.Contains("SysName"))
                dt.Columns.Add("SysName", typeof(string));
            if (!dt.Columns.Contains("DbType"))
                dt.Columns.Add("DbType", typeof(int)).DefaultValue = 0;
            if (!dt.Columns.Contains("ServerIP"))
                dt.Columns.Add("ServerIP", typeof(string));
            if (!dt.Columns.Contains("DbName"))
                dt.Columns.Add("DbName", typeof(string));
            if (!dt.Columns.Contains("UserName"))
                dt.Columns.Add("UserName", typeof(string));
            if (!dt.Columns.Contains("PassWord"))
                dt.Columns.Add("PassWord", typeof(string));
            if (!dt.Columns.Contains("CommandTimeout"))
                dt.Columns.Add("CommandTimeout", typeof(int)).DefaultValue = 0;
            if (!dt.Columns.Contains("PageRecordCout"))
                dt.Columns.Add("PageRecordCout", typeof(int)).DefaultValue = 2000;
            if (!dt.Columns.Contains("RowCondCount"))
                dt.Columns.Add("RowCondCount", typeof(int)).DefaultValue = 0;
            if (!dt.Columns.Contains("LoginUserName"))
                dt.Columns.Add("LoginUserName", typeof(string));
            if (!dt.Columns.Contains("LoginPassWord"))
                dt.Columns.Add("LoginPassWord", typeof(string));
            if (!dt.Columns.Contains("SubSystem"))
                dt.Columns.Add("SubSystem", typeof(string));
            if (!dt.Columns.Contains("DB_NO"))
                dt.Columns.Add("DB_NO", typeof(int)).DefaultValue = -1;
            if (!dt.Columns.Contains("WebServiceProxy"))
                dt.Columns.Add("WebServiceProxy", typeof(string));
            if (!dt.Columns.Contains("EnableProxy"))
                dt.Columns.Add("EnableProxy", typeof(int)).DefaultValue = 0;
            if (!dt.Columns.Contains("ChineseConversion"))
                dt.Columns.Add("ChineseConversion", typeof(int)).DefaultValue = 256;
            if (dt.Rows.Count == 0)
                dt.Rows.Add(dt.NewRow());
            foreach (DataColumn dcol in dt.Columns)
            {
                if (dcol.DefaultValue != null && dt.Rows[0][dcol].Equals(DBNull.Value))
                {
                    dt.Rows[0][dcol] = dcol.DefaultValue;
                }
            }
        }

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        private ParamSet()
        {
        }

        /// <summary>
        /// 获取参数表
        /// </summary>
        public static DataTable ParamTable
        {
            get
            {
                return dset.Tables["ParamTable"];
            }
        }
        /// <summary>
        /// 获取或设置子系统参数表
        /// </summary>
        public static DataTable SubSystemTable
        {
            get
            {
                return dset.Tables["SubSystemTable"];
            }
            set
            {
                if (dset.Tables.Contains("SubSystemTable"))
                    dset.Tables.Remove("SubSystemTable");
                DataTable dt = value;
                dt.TableName = "SubSystemTable";
                dset.Tables.Add(dt);
            }
        }

        /// <summary>
        /// 获取参数信息
        /// </summary>
        public static string GetParamData(string columnname)
        {
            return ParamTable.Rows[0][columnname].ToString();
        }

        /// <summary>
        /// 设置参数信息
        /// </summary>
        public static void SetParamData(string columnname, string value)
        {
            ParamTable.Rows[0][columnname] = value;
        }

        /// <summary>
        /// 数据库名称
        /// </summary>
        public static string DbName
        {
            get
            {
                return GetParamData("DbName");
            }
        }

        /// <summary>
        /// WebService代理
        /// </summary>
        public static string WebServiceProxy
        {
            get
            {
                return GetParamData("WebServiceProxy");
            }
        }

        /// <summary>
        /// 是否启用代理
        /// </summary>
        public static bool EnableProxy
        {
            get
            {
                return GetParamData("EnableProxy").Equals("1");
            }
            set
            {
                SetParamData("EnableProxy", (value ? "1" : "0"));
            }
        }

        /// <summary>
        /// 子系统编号
        /// </summary>
        public static string SubSystem
        {
            get
            {
                return GetParamData("SubSystem");
            }
            set
            {
                SetParamData("SubSystem", value);
            }
        }

        /// <summary>
        /// 当前数据库编号
        /// </summary>
        public static int DB_NO
        {
            get
            {
                int db_no = -1;
                if (!int.TryParse(GetParamData("DB_NO"), out db_no))
                    db_no = -1;
                return db_no;
            }
            set
            {
                SetParamData("DB_NO", value.ToString());
            }
        }

        /// <summary>
        /// 界面语言
        /// </summary>
        public static int ChineseConversion
        {
            get
            {
                return int.Parse(GetParamData("ChineseConversion"));
            }
        }

        /// <summary>
        /// 每页显示记录数，0表示不限制
        /// </summary>
        public static int PageRecordCout
        {
            get
            {
                return int.Parse(GetParamData("PageRecordCout"));
            }
        }

        /// <summary>
        /// 每行条件数，0表示自动
        /// </summary>
        public static int RowCondCount
        {
            get
            {
                return int.Parse(GetParamData("RowCondCount"));
            }
        }

        /// <summary>
        /// 等待命令执行的时间（以秒为单位）。0表示没限制。
        /// </summary>
        public static int CommandTimeout
        {
            get
            {
                return int.Parse(GetParamData("CommandTimeout"));
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        public static void Save()
        {
            DataSetIO.SaveDataSetToXmlZip(dset, AbstractInterFace.StartupPath + filename);
        }

        /// <summary>
        /// 获取数据库项目
        /// </summary>
        /// <returns>数据库项目</returns>
        public static DbItem GetDbItem()
        {
            DbItem item = new DbItem();
            item.SUB_SYSTEM = "";
            item.DB_TYPE = (DataBaseType)int.Parse(GetParamData("DbType"));
            item.DB_IP = GetParamData("ServerIP");
            item.DB_NAME = GetParamData("DbName");
            item.USER_NAME = GetParamData("UserName");
            item.USER_PASSWORD = GetParamData("PassWord");
            return item;
        }
    }
}
